package emi99

// 支付2.0接口

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-pay/gopay"
)

// Micro 条码支付
func (c *Client) Micro(ctx context.Context, data gopay.BodyMap) (res *MicroRsp, err error) {
	if err = data.CheckEmptyError("amount", "authCode", "outOrderNo", "subject"); err != nil {
		return nil, err
	}
	if v := data.GetString("customerNo"); v == gopay.NULL {
		data.Set("customerNo", c.customerNo)
	}
	if v := data.GetString("device"); v == gopay.NULL {
		data.Set("device", c.device)
	}
	var bs []byte
	if bs, err = c.doPost(ctx, PayUriMicro, data); err != nil {
		return nil, err
	}

	rsp := new(BaseRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp); err != nil {
		return nil, err
	}

	if err := json.Unmarshal([]byte(rsp.Data), &res); err != nil {
		return nil, err
	}
	return
}

// Query 支付查询
func (c *Client) Query(ctx context.Context, bm gopay.BodyMap) (res *QueryRsp, err error) {
	if bm.GetString("orderNo") == "" && bm.GetString("outOrderNo") == "" {
		return nil, fmt.Errorf("[%w], %v", gopay.MissParamErr, "orderNo or outOrderNo")
	}
	if v := bm.GetString("customerNo"); v == gopay.NULL {
		bm.Set("customerNo", c.customerNo)
	}
	if v := bm.GetString("device"); v == gopay.NULL {
		bm.Set("device", c.device)
	}
	var bs []byte
	if bs, err = c.doPost(ctx, PayUriQuery, bm); err != nil {
		return nil, err
	}

	rsp := new(BaseRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp); err != nil {
		return nil, err
	}

	if err := json.Unmarshal([]byte(rsp.Data), &res); err != nil {
		return nil, err
	}
	return
}

// Refund 退款申请
func (c *Client) Refund(ctx context.Context, bm gopay.BodyMap) (res *RefundRsp, err error) {
	err = bm.CheckEmptyError("originalOutOrderNo")
	if err != nil {
		return nil, err
	}
	if v := bm.GetString("customerNo"); v == gopay.NULL {
		bm.Set("customerNo", c.customerNo)
	}
	if v := bm.GetString("device"); v == gopay.NULL {
		bm.Set("device", c.device)
	}
	var bs []byte
	if bs, err = c.doPost(ctx, PayUriRefund, bm); err != nil {
		return nil, err
	}

	rsp := new(BaseRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp); err != nil {
		return nil, err
	}

	if err := json.Unmarshal([]byte(rsp.Data), &res); err != nil {
		return nil, err
	}
	return
}

// RefundQuery 退款订单查询
func (c *Client) RefundQuery(ctx context.Context, bm gopay.BodyMap) (res *RefundQueryRsp, err error) {
	if bm.GetString("orderNo") == "" && bm.GetString("outOrderNo") == "" {
		return nil, fmt.Errorf("[%w], %v", gopay.MissParamErr, "orderNo or outOrderNo")
	}
	if v := bm.GetString("customerNo"); v == gopay.NULL {
		bm.Set("customerNo", c.customerNo)
	}
	if v := bm.GetString("device"); v == gopay.NULL {
		bm.Set("device", c.device)
	}
	var bs []byte
	if bs, err = c.doPost(ctx, PayUriRefundQuery, bm); err != nil {
		return nil, err
	}

	rsp := new(BaseRsp)
	if err = json.Unmarshal(bs, rsp); err != nil {
		return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
	}
	if err := bizErrCheck(rsp); err != nil {
		return nil, err
	}

	if err := json.Unmarshal([]byte(rsp.Data), &res); err != nil {
		return nil, err
	}
	return
}
